<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace web\api\controller;

/**
 * Description of NewsAction
 *
 * @author shilinqing
 */
class NewsAction extends UserBase {

    public function like() {
        if (!IS_POST) {
            return $this->failJSON('Illegal Request');
        }
        $news_id = $this->getParamVal('news_id', true, 'int');
        try {
            $m = new \addons\news\model\News();
            $actionRecordM = new \addons\news\model\NewsActionRecord();
            $record = $actionRecordM->getUserRecord($this->user_id, $news_id);
            $flag = 1;
            $actionRecordM->startTrans();
            if (empty($record)) {
                $record['user_id'] = $this->user_id;
                $record['news_id'] = $news_id;
                $record['is_like'] = $flag;
                $ret = $actionRecordM->add($record);
            } else {
                if ($record['is_like'] == 1) {
                    $flag = 0;
                }
                $record['is_like'] = $flag;
                $ret = $actionRecordM->save($record);
            }
            if ($ret > 0) {
                if ($flag == 1) {
                    $m->where('id=' . $news_id)->setInc('like_nums', 1);
                } else {
                    $m->where('id=' . $news_id)->setDec('like_nums', 1);
                }
            }

            $actionRecordM->commit();
            return $this->successJSON($flag);
        } catch (\Exception $ex) {
            $actionRecordM->rollback();
            return $this->failJSON($ex->getMessage());
        }
    }

    public function collect() {
        if (!IS_POST) {
            return $this->failJSON('Illegal Request');
        }
        try {
            $news_id = $this->getParamVal('news_id', true, 'int');
            $m = new \addons\news\model\News();
            $actionRecordM = new \addons\news\model\NewsActionRecord();
            $record = $actionRecordM->getUserRecord($this->user_id, $news_id);
            $flag = 1;
            if (empty($record)) {
                $record['user_id'] = $this->user_id;
                $record['news_id'] = $news_id;
                $record['is_collect'] = $flag;
                $ret = $actionRecordM->add($record);
            } else {
                if ($record['is_collect'] == 1) {
                    $flag = 0;
                }
                $record['is_collect'] = $flag;
                $ret = $actionRecordM->save($record);
            }
            if ($ret > 0) {
                if ($flag == 1) {
                    $m->where('id=' . $news_id)->setInc('collect_nums', 1);
                } else {
                    $m->where('id=' . $news_id)->setDec('collect_nums', 1);
                }
            }
            return $this->successJSON($flag);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
    
    public function commentLike(){
        try{
            $comment_id = $this->getParamVal('comment_id',true, 'int');
            $flag = true;
            $m = new \addons\news\model\Comment();
            $likeRecordM = new \addons\news\model\CommLikeRecord();
            $user_like = $likeRecordM->where('user_id='.$this->user_id)->find();
            if(empty($user_like)){
                //未点赞过
                $user_like['user_id'] = $this->user_id;
                $user_like['comment_ids'] = $comment_id;
                $likeRecordM->save($user_like);
//                    点赞数+1
                $m->where('id='.$comment_id)->setInc('like_nums', 1);
            }else{
                //有点赞记录
                $comment_ids = $user_like['comment_ids'];
                $comment_ids = explode(',', $comment_ids);
                foreach($comment_ids as $k => $id){
                    if($id == $comment_id){
                        //存在记录 取消点赞
                        $flag = false;
                        unset($comment_ids[$k]);
                    }
                }
                if(!empty($comment_ids)){
                    $user_like['comment_ids'] = implode(',', $comment_ids);
                    if($flag){
                        $user_like['comment_ids'] .= ','.$comment_id;
                        $m->where('id='.$comment_id)->setInc('like_nums', 1);
                    }else{
                        //取消, 点赞数-1
                        $m->where('id='.$comment_id)->setDec('like_nums', 1);
                    }
                    $likeRecordM->save($user_like);
                }else{
                    $likeRecordM->deleteData($user_like['id']);
                    $m->where('id='.$comment_id)->setDec('like_nums', 1);
                }

            }
            return $this->successJSON($flag);


        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
    

}
